package com.tansuo365.test1.service.otherservice.impl;

import com.tansuo365.test1.bean.otherbean.HuiShouDetails;
import com.tansuo365.test1.bean.otherbean.HuiShouZhixing;
import com.tansuo365.test1.bean.otherbean.HuiShouZong;
import com.tansuo365.test1.mapper.othermapper.HuiShouDetailsMapper;
import com.tansuo365.test1.mapper.othermapper.HuiShouZhixingMapper;
import com.tansuo365.test1.service.otherservice.HuishouDetailsService;
import com.tansuo365.test1.service.otherservice.HuishouZhixingService;
import com.tansuo365.test1.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HuishouZhixingServiceImpl implements HuishouZhixingService {

    @Resource
    private HuiShouZhixingMapper huiShouZhixingMapper;
    @Resource
    private HuiShouDetailsMapper huiShouDetailsMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return huiShouZhixingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(HuiShouZhixing huiShouZhixing) {
        return huiShouZhixingMapper.insertSelective(huiShouZhixing);
    }

    /**
     * 回收单绑定出库单
     * 回收新加入退库量的计算
     * 出库量-退库量-回收量=未回收量
     * 退库量从该出库单对应的出库执行汇总中获取它的退库量
     * <p>
     * 现在创建回收单 都已经调过来了退库数量
     * 保存时同时在保存时可以保存退库量
     * <p>
     * 一切都以新数据为准
     *
     * @param huiShouDetailsList
     * @param huiShouZong
     * @return
     */
    @Override
    @Transactional
    public int saveHuiShouBindChuku(List<HuiShouDetails> huiShouDetailsList, HuiShouZong huiShouZong) {
        int i = 0;
        int insertResultCode = 0;
        //新进入的回收详细,如果是首批次录入,以及同单号的第N次录入
        //
        for (HuiShouDetails huiShouDetail : huiShouDetailsList) {
            if (StringUtil.isNotEmpty(huiShouDetail.getDuiyingchukudanhao())) {
                //根据单号和物料名称获取回收执行数据
                HuiShouZhixing huiShouZhixingData = huiShouZhixingMapper.listHuishouZhixingByDanhaoAndMingcheng(
                        huiShouDetail.getDuiyingchukudanhao(), huiShouDetail.getMingchengguige()
                );
                HuiShouZhixing huiShouZhixing = new HuiShouZhixing();
                //设置参数
                huiShouZhixing.setDuiyingchukudanhao(huiShouDetail.getDuiyingchukudanhao());
                huiShouZhixing.setMingchengguige(huiShouDetail.getMingchengguige());
                huiShouZhixing.setDanwei(huiShouDetail.getDanwei());
                Double chukuhuishoushuliang = huiShouDetail.getNumber(); //出库数量(可回收的)
                Double tuikushuliang = huiShouDetail.getTuikushuliang();
                Double yihuishoushuliang = huiShouDetail.getHuishoushuliang();
                //未回收量= 出库单中绑定的这条物料是可回收的 出库多少 - 用户填入的已回收量
                Double weihuishouliang = chukuhuishoushuliang - tuikushuliang - yihuishoushuliang;
                huiShouZhixing.setChukushuliang(chukuhuishoushuliang);
                huiShouZhixing.setHuishoushuliang(yihuishoushuliang);
                huiShouZhixing.setTuikushuliang(tuikushuliang);
                huiShouZhixing.setWeihuishoushuliang(weihuishouliang);
                huiShouZhixing.setLiangliaodanwei(huiShouZong.getGuihuandanwei());
                huiShouZhixing.setLiangliaoren(huiShouZong.getGuihuanren());
                huiShouZhixing.setDuiyinghuishoudanhao(huiShouZong.getHuishoudanhao());
//            huiShouZhixing.setChukushijian(huiShouZong.get);
                String huishouzhuangtai = "";
                double preErrorKey = 1e-6; //1*10的-6次方即0.000001
                double key1 = 0.0000001d;
                double key2 = 0d;
                if (huiShouZhixingData == null) {
                    //如果是首批次的回收单录入,则这次的退库数量是一定的
                    i++;
                    /*加上退库数量 以及 回收的可回收物品 如果数量 跟出库的一致 则说明 全部回收了 因为已经不用再交付回收的物品了
                    而如果回收量0,退库量0,那么肯定是未回收.
                    如果仅仅是退库了,但是未进行回收,那么应该是不属于部分回收了.
                    假设出库10个,退库10个可回收,没有执行回收单操作,肯定就没有这个单据.也算不上回收.
                    那么部分回收只能定义在进行了回收数量的增加,就算该单子有退库数量,不算作部分回收.
                    超量的话,就是退库的数量+可回收的退回超越了原本的出库量*/
                    if((chukuhuishoushuliang-tuikushuliang<preErrorKey) && (weihuishouliang <preErrorKey)){
                        huishouzhuangtai = "全部退库";
                    }else if (Math.abs(chukuhuishoushuliang - tuikushuliang - yihuishoushuliang) < preErrorKey && (weihuishouliang == 0)) {
                        huishouzhuangtai = "全部回收";
                    } else if ((yihuishoushuliang + tuikushuliang == 0) && (chukuhuishoushuliang != 0)) {
                        huishouzhuangtai = "未回收";
                    } else if (((tuikushuliang + yihuishoushuliang) < chukuhuishoushuliang) && yihuishoushuliang > 0) {
                        huishouzhuangtai = "部分回收";
                    }  else if (((tuikushuliang + yihuishoushuliang) < chukuhuishoushuliang) && tuikushuliang > 0) {
                        huishouzhuangtai = "部分退库";
                    } else if ((tuikushuliang + yihuishoushuliang) > chukuhuishoushuliang) {
                        huishouzhuangtai = "超量回收";
                    }
                    huiShouZhixing.setHuishouzhuangtai(huishouzhuangtai);
                    int i1 = huiShouZhixingMapper.insertSelective(huiShouZhixing);
                    insertResultCode += i1;
                } else {
                    //如果是第N次回收单录入,这次的退库数量也是获取的最新的,所以在update时,退库为最新的,而不需要累加
                    //直接拿现在最新的退库数量,替换原来的退库数量即可
                    i++;
                    if((chukuhuishoushuliang-tuikushuliang<preErrorKey) && (weihuishouliang <preErrorKey)){
                        huishouzhuangtai = "全部退库";
                    }else if (Math.abs(chukuhuishoushuliang - tuikushuliang - yihuishoushuliang - huiShouZhixingData.getHuishoushuliang()) < preErrorKey && (huiShouZhixingData.getChukushuliang() > 0)) {
                        huishouzhuangtai = "全部回收";
                    } else if ((yihuishoushuliang + tuikushuliang + huiShouZhixingData.getHuishoushuliang() == 0) && (chukuhuishoushuliang != 0)) {
                        huishouzhuangtai = "未回收";
                    } else if (((tuikushuliang + yihuishoushuliang + huiShouZhixingData.getHuishoushuliang()) < chukuhuishoushuliang) && yihuishoushuliang > 0) {
                        huishouzhuangtai = "部分回收";
                    }  else if (((tuikushuliang + yihuishoushuliang) < chukuhuishoushuliang) && tuikushuliang > 0) {
                        huishouzhuangtai = "部分退库";
                    } else if ((tuikushuliang + yihuishoushuliang + huiShouZhixingData.getHuishoushuliang()) > chukuhuishoushuliang) {
                        huishouzhuangtai = "超量回收";
                    }else if((chukuhuishoushuliang-tuikushuliang<preErrorKey) && (weihuishouliang <preErrorKey)){
                        huishouzhuangtai = "全部退库";
                    }
                    huiShouZhixing.setHuishouzhuangtai(huishouzhuangtai);
                    huiShouZhixing.setId(huiShouZhixingData.getId());
                    int i1 = huiShouZhixingMapper.updateZhixingChangeHuishouWeiHuishouAndZhuangtai(huiShouZhixing);
                    insertResultCode += i1;
                }
            } else {
                i++;
                insertResultCode++;
            }
        }
        if (insertResultCode == i) {
            return i;
        } else if ((insertResultCode != i) && (insertResultCode > 0 && i > 0)) {
            return -1;
        } else if (insertResultCode == 0) {
            return 0;
        } else {
            return -2;
        }
    }

    @Override
    public HuiShouZhixing selectByPrimaryKey(Integer id) {
        return huiShouZhixingMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<HuiShouZhixing> listHuiShouZhixingSelective(HuiShouZhixing huiShouZhixing) {
        return huiShouZhixingMapper.listHuiShouZhixingSelective(huiShouZhixing);
    }

    @Override
    public int updateByPrimaryKeySelective(HuiShouZhixing huiShouZhixing) {
        return huiShouZhixingMapper.updateByPrimaryKeySelective(huiShouZhixing);
    }

    @Override
    public int deleteByIdArr(Integer[] ids) {
        return huiShouZhixingMapper.deleteByIdArr(ids);
    }

    @Override
    @Transactional
    public int deleteTheHuishouFromIdArr(Integer[] ids) {
        int updateCode = 0;
        List<HuiShouZhixing> huishouzhixingList = huiShouZhixingMapper.getHuishouZhixingByZongIds(ids);
        if (huishouzhixingList != null) {
            for (HuiShouZhixing huishouzhixing : huishouzhixingList) {
                updateCode = huiShouZhixingMapper.updateZhixingChangeHuishouWeiHuishouAndZhuangtaiOnDelete(huishouzhixing);
            }
        }
        if (huishouzhixingList.size() == updateCode) {
            return updateCode;
        } else {
            return 0;
        }
    }

    @Override
    public int deleteHuishouBindChuku(Integer[] ids) {
        int changeCode = 0;
        for (Integer id : ids) {
            List<HuiShouDetails> huiShouDetailList = huiShouDetailsMapper.listHuiShouDetailsByZongId(id);
            for (HuiShouDetails huishouDetails : huiShouDetailList) {
                HuiShouZhixing huiShouZhixingData = huiShouZhixingMapper.listHuishouZhixingByDanhaoAndMingcheng(
                        huishouDetails.getDuiyingchukudanhao(), huishouDetails.getMingchengguige());
                String huishouzhuangtai = "";
                double preErrorKey = 1e-6; //1*10的-6次方即0.000001
                double key1 = 0.0000001d;
                double key2 = 0d;
                Double chukuhuishoushuliang = huiShouZhixingData.getChukushuliang();
                Double tuikushuliang = huiShouZhixingData.getTuikushuliang();
                Double yihuishoushuliang = huiShouZhixingData.getHuishoushuliang();
                Double huishoushuliang = huishouDetails.getHuishoushuliang();
                if (Math.abs(chukuhuishoushuliang - tuikushuliang - yihuishoushuliang - huishoushuliang) < preErrorKey && (chukuhuishoushuliang > 0)) {
                    huishouzhuangtai = "全部回收";
                } else if ((yihuishoushuliang + tuikushuliang - huishoushuliang == 0) && (chukuhuishoushuliang != 0)) {
                    huishouzhuangtai = "未回收";
                } else if (((tuikushuliang + yihuishoushuliang - huishoushuliang) < chukuhuishoushuliang) && yihuishoushuliang > 0) {
                    huishouzhuangtai = "部分回收";
                } else if ((tuikushuliang + yihuishoushuliang - huishoushuliang) > chukuhuishoushuliang) {
                    huishouzhuangtai = "超量回收";
                }
                huiShouZhixingData.setHuishoushuliang(-huishoushuliang);
                huiShouZhixingData.setHuishouzhuangtai(huishouzhuangtai);
                huiShouZhixingData.setId(huiShouZhixingData.getId());
                int i1 = huiShouZhixingMapper.updateZhixingChangeHuishouWeiHuishouAndZhuangtai(huiShouZhixingData);
                changeCode += i1;
            }
        }
        if (changeCode > 0) {
            return changeCode;
        } else {
            return 0;
        }
    }

    @Override
    public int deleteByDuiyingChukudans(List<String> duiyingchukudanhaos) {
        return huiShouZhixingMapper.deleteByDuiyingChukudans(duiyingchukudanhaos);
    }
}
